package matchs

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	common_response "github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/matchs/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/matchs/response"
	"github.com/flipped-aurora/gin-vue-admin/server/service/matchs"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type SearchApi struct{}

// CreateSearchTask 创建寻访任务
// @Tags MatchSearch
// @Summary 创建寻访任务
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.CreateSearchTaskReq true "创建寻访任务参数"
// @Success 200 {object} response.Response{data=uint64,msg=string} "创建成功，返回任务ID"
// @Router /api/v1/match/search/create [post]
func (a *SearchApi) CreateSearchTask(c *gin.Context) {
	var req request.CreateSearchTaskReq
	if err := c.ShouldBindJSON(&req); err != nil {
		common_response.FailWithMessage("参数绑定失败: "+err.Error(), c)
		return
	}

	// 从JWT中获取用户ID
	userId := utils.GetUserID(c)

	// 调用服务层创建任务
	taskId, err := matchs.SearchServiceApp.CreateSearchTask(c.Request.Context(), uint64(userId), req.TargetProfessionalIDs, req.FilterCity, req.FilterGrade, req.SortStrategy)
	if err != nil {
		global.GVA_LOG.Error("创建寻访任务失败!", zap.Error(err))
		common_response.FailWithMessage(err.Error(), c)
		return
	}

	common_response.OkWithData(taskId, c)
}

// GetUserSearchTaskList 获取用户发起的寻访任务列表
// @Tags MatchSearch
// @Summary 获取用户发起的寻访任务列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param status query int false "任务状态（0-待处理 1-已推荐 2-已沟通 3-已结束 4-已取消 5-推荐失败）"
// @Param page query int true "页码"
// @Param size query int true "每页条数"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功，返回任务列表和总数"
// @Router /api/v1/match/search/list [get]
func (a *SearchApi) GetUserSearchTaskList(c *gin.Context) {
	var req request.GetSearchTaskListReq
	if err := c.ShouldBindQuery(&req); err != nil {
		common_response.FailWithMessage("参数绑定失败: "+err.Error(), c)
		return
	}

	// 从JWT中获取用户ID
	userId := utils.GetUserID(c)

	// 调用服务层获取任务列表
	taskList, total, err := matchs.SearchServiceApp.GetUserSearchTaskList(c.Request.Context(), uint64(userId), req.Status, req.Page, req.PageSize)
	if err != nil {
		global.GVA_LOG.Error("获取寻访任务列表失败!", zap.Error(err))
		common_response.FailWithMessage(err.Error(), c)
		return
	}

	// 封装分页结果并返回
	common_response.OkWithDetailed(common_response.PageResult{
		List:     taskList,
		Total:    total,
		Page:     req.Page,
		PageSize: req.PageSize,
	}, "获取成功", c)
}

// GetSearchTaskRecommendList 获取寻访任务推荐的用户列表
// @Tags MatchSearch
// @Summary 获取寻访任务推荐的用户列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param taskId query int true "任务ID"
// @Param page query int true "页码"
// @Param size query int true "每页条数"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功，返回推荐用户列表和总数"
// @Router /api/v1/match/search/recommend/list [get]
func (a *SearchApi) GetSearchTaskRecommendList(c *gin.Context) {
	var req request.GetRecommendListReq
	if err := c.ShouldBindQuery(&req); err != nil {
		common_response.FailWithMessage("参数绑定失败: "+err.Error(), c)
		return
	}

	// 从JWT中获取用户ID
	userId := utils.GetUserID(c)

	// 调用服务层获取推荐用户列表
	recommendList, total, err := matchs.SearchServiceApp.GetSearchTaskRecommendList(c.Request.Context(), uint64(userId), req.TaskId, req.Page, req.PageSize)
	if err != nil {
		global.GVA_LOG.Error("获取推荐用户列表失败!", zap.Error(err))
		common_response.FailWithMessage(err.Error(), c)
		return
	}

	// 封装分页结果并返回
	common_response.OkWithDetailed(common_response.PageResult{
		List:     recommendList,
		Total:    total,
		Page:     req.Page,
		PageSize: req.PageSize,
	}, "获取成功", c)
}

// SkipRecommendUser 跳过推荐用户
// @Tags MatchSearch
// @Summary 跳过推荐用户
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.SkipRecommendReq true "跳过推荐用户参数"
// @Success 200 {object} response.Response{msg=string} "操作成功"
// @Router /api/v1/match/search/recommend/skip [post]
func (a *SearchApi) SkipRecommendUser(c *gin.Context) {
	var req request.SkipRecommendReq
	if err := c.ShouldBindJSON(&req); err != nil {
		common_response.FailWithMessage("参数绑定失败: "+err.Error(), c)
		return
	}

	// 从JWT中获取用户ID
	userId := utils.GetUserID(c)

	// 调用服务层跳过推荐用户
	err := matchs.SearchServiceApp.SkipRecommendUser(c.Request.Context(), uint64(userId), req.TaskId, req.RecommendId)
	if err != nil {
		global.GVA_LOG.Error("跳过推荐用户失败!", zap.Error(err))
		common_response.FailWithMessage(err.Error(), c)
		return
	}

	common_response.OkWithMessage("操作成功", c)
}

// CommunicateWithRecommendUser 沟通推荐用户
// @Tags MatchSearch
// @Summary 沟通推荐用户
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.CommunicateRecommendReq true "沟通推荐用户参数"
// @Success 200 {object} response.Response{data=response.CommunicateResultVO,msg=string} "操作成功，返回聊天ID"
// @Router /api/v1/match/search/recommend/communicate [post]
func (a *SearchApi) CommunicateWithRecommendUser(c *gin.Context) {
	var req request.CommunicateRecommendReq
	if err := c.ShouldBindJSON(&req); err != nil {
		common_response.FailWithMessage("参数绑定失败: "+err.Error(), c)
		return
	}

	// 从JWT中获取用户ID
	userId := utils.GetUserID(c)

	// 调用服务层沟通推荐用户
	chatId, err := matchs.SearchServiceApp.CommunicateWithRecommendUser(c.Request.Context(), uint64(userId), req.TaskId, req.RecommendId, req.FirstMessage)
	if err != nil {
		global.GVA_LOG.Error("沟通推荐用户失败!", zap.Error(err))
		common_response.FailWithMessage(err.Error(), c)
		return
	}

	common_response.OkWithData(response.CommunicateResultVO{
		ChatId: chatId,
	}, c)
}

// CompleteSearchTask 完成寻访任务并评分
// @Tags MatchSearch
// @Summary 完成寻访任务并评分
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.CompleteSearchTaskReq true "完成寻访任务参数"
// @Success 200 {object} response.Response{msg=string} "操作成功"
// @Router /api/v1/match/search/complete [post]
func (a *SearchApi) CompleteSearchTask(c *gin.Context) {
	var req request.CompleteSearchTaskReq
	if err := c.ShouldBindJSON(&req); err != nil {
		common_response.FailWithMessage("参数绑定失败: "+err.Error(), c)
		return
	}

	// 从JWT中获取用户ID
	userId := utils.GetUserID(c)

	// 调用服务层完成寻访任务
	err := matchs.SearchServiceApp.CompleteSearchTask(c.Request.Context(), uint64(userId), req.TaskId, req.RecommendId, req.Score, req.Comment)
	if err != nil {
		global.GVA_LOG.Error("完成寻访任务失败!", zap.Error(err))
		common_response.FailWithMessage(err.Error(), c)
		return
	}

	common_response.OkWithMessage("操作成功", c)
}
